#############################################################
## Fraga, Juenke, and Shah 2019: One Run Leads to Another?
## Replication Code: December 26, 2018
## Figure 1: Candidate Emergence by Composition and Context
#############################################################

# Required Libraries
require(ggplot2)

# Load Data #
sl_data <- read.csv("FragaJuenkeShah_JOP_Data.csv", stringsAsFactors=FALSE)

# Remove all districts where 1+ candidates have no race data
sl_data <- subset(sl_data, unknown_cand == 0)

# Change candidate variable to a binary, 0 = no candidate, 1 = one or more candidates
sl_data$white_cand[sl_data$white_cand > 1] <- 1
sl_data$black_cand[sl_data$black_cand > 1] <- 1
sl_data$latino_cand[sl_data$latino_cand > 1] <- 1
sl_data$asian_cand[sl_data$asian_cand > 1] <- 1

## Fig1a, Upper Left Panel of Figure 1: Whites
sl_dataY <- subset(sl_data, white_incprop >= 0.25)
sl_dataN <- subset(sl_data, white_incprop < 0.25)

fig1a <- ggplot(data=sl_data, aes(x=white_pct, y=white_cand))
fig1a <- fig1a + geom_segment(aes(y=0.5, yend=0.5, x=-Inf, xend=Inf), size=0.75, linetype=1, color="gray80")
fig1a <- fig1a + stat_smooth(data=sl_dataN, method = "loess", size=2, color="black")
fig1a <- fig1a + stat_smooth(data=sl_dataY, method = "loess", size=2, color="gray40", linetype="longdash")
fig1a <- fig1a + ggtitle("Whites")
fig1a <- fig1a + scale_x_continuous("Percent White in State Legislative District")
fig1a <- fig1a + scale_y_continuous("Probability of White Candidate")
fig1a <- fig1a + coord_cartesian(ylim=c(-0.01, 1.01))
fig1a <- fig1a + theme_bw() + theme(
	panel.grid.minor=element_blank(), 
	panel.grid.major=element_blank(),
	legend.title=element_blank())
fig1a

## Fig1b, Upper Right Panel of Figure 1: African-Americans
sl_dataY <- subset(sl_data, black_incprop >= 0.25)
sl_dataN <- subset(sl_data, black_incprop < 0.25)

fig1b <- ggplot(data=sl_data, aes(x=black_pct, y=black_cand))
fig1b <- fig1b + geom_segment(aes(y=0.5, yend=0.5, x=-Inf, xend=Inf), size=0.75, linetype=1, color="gray80")
fig1b <- fig1b + stat_smooth(data=sl_dataN, method = "loess", size=2, color="black")
fig1b <- fig1b + stat_smooth(data=sl_dataY, method = "loess", size=2, color="gray40", linetype="longdash")
fig1b <- fig1b + ggtitle("African-Americans")
fig1b <- fig1b + scale_x_continuous("Percent Black in State Legislative District")
fig1b <- fig1b + scale_y_continuous("Probability of Black Candidate")
fig1b <- fig1b + coord_cartesian(ylim=c(-0.01, 1.01))
fig1b <- fig1b + theme_bw() + theme(
	panel.grid.minor=element_blank(), 
	panel.grid.major=element_blank(),
	legend.title=element_blank())
fig1b

## Fig1c, Lower Left Panel of Figure 1: Latinos
sl_dataY <- subset(sl_data, latino_incprop >= 0.25)
sl_dataN <- subset(sl_data, latino_incprop < 0.25)

fig1c <- ggplot(data=sl_data, aes(x=latino_pct, y=latino_cand))
fig1c <- fig1c + geom_segment(aes(y=0.5, yend=0.5, x=-Inf, xend=Inf), size=0.75, linetype=1, color="gray80")
fig1c <- fig1c + stat_smooth(data=sl_dataN, method = "loess", size=2, color="black")
fig1c <- fig1c + stat_smooth(data=sl_dataY, method = "loess", size=2, color="gray40", linetype="longdash")
fig1c <- fig1c + ggtitle("Latinos")
fig1c <- fig1c + scale_x_continuous("Percent Latino in State Legislative District")
fig1c <- fig1c + scale_y_continuous("Probability of Latino Candidate")
fig1c <- fig1c + coord_cartesian(ylim=c(-0.01, 1.01))
fig1c <- fig1c + theme_bw() + theme(
	panel.grid.minor=element_blank(), 
	panel.grid.major=element_blank(),
	legend.title=element_blank())
fig1c

## Fig1d, Lower Right Panel of Figure 1: Asians
sl_dataY <- subset(sl_data, asian_incprop >= 0.25)
sl_dataN <- subset(sl_data, asian_incprop < 0.25)

fig1d <- ggplot(data=sl_data, aes(x=asian_pct, y=asian_cand))
fig1d <- fig1d + geom_segment(aes(y=0.5, yend=0.5, x=-Inf, xend=Inf), size=0.75, linetype=1, color="gray80")
fig1d <- fig1d + stat_smooth(data=sl_dataN, method = "loess", size=2, color="black")
fig1d <- fig1d + stat_smooth(data=sl_dataY, method = "loess", size=2, color="gray40", linetype="longdash")
fig1d <- fig1d + ggtitle("Asians")
fig1d <- fig1d + scale_x_continuous("Percent Asian in State Legislative District")
fig1d <- fig1d + scale_y_continuous("Probability of Asian Candidate")
fig1d <- fig1d + coord_cartesian(ylim=c(-0.01, 1.01))
fig1d <- fig1d + theme_bw() + theme(
	panel.grid.minor=element_blank(), 
	panel.grid.major=element_blank(),
	legend.title=element_blank())
fig1d